package com.mxx.common.thread;

/**
 * @author: mxx
 * @date: 2021/8/19
 * @time: 11:16
 * @Description: 描述信息
 */

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.function.BiConsumer;

/**
 * 数据分化后批次进行单个处理
 *
 * @param <T>
 */
@Slf4j
public class Batch<T> implements Runnable {
    private List<T> batchDataList;
    private BiConsumer<T, Integer> consumer;
    private int batchStart;

    /**
     * @param batchDataList 批数据集合
     * @param batchStart    批数据起始位置
     * @param consumer      执行方法
     */

    public Batch(List<T> batchDataList, int batchStart, BiConsumer<T, Integer> consumer) {
        this.batchDataList = batchDataList;
        this.batchStart = batchStart;
        this.consumer = consumer;
    }

    @Override
    public void run() {
        try {
            int start = batchStart;
            while (start < batchDataList.size()) {
                consumer.accept(batchDataList.get(start), start++);
            }
        } catch (Exception ex) {
            log.error("execute batch exception", ex);
        }
    }

}
